package homework1;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 1. 编程题
 * 基于学生信息管理系统增加以下两个功能：
 * a.自定义学号异常类和年龄异常类，并在该成员变量不合理时产生异常对象并抛出。
 * b.当系统退出时将 List 集合中所有学生信息写入到文件中，当系统启动时读取文件中所有学生信息到 List 集合中。
 */
public class ListTest {
    public static void main(String[] args) {

        //1，声明一个集合存放学生信息
        List<StudentInformation> list = new ArrayList<>();

        //打印字符界面提示用户选择相应的功能
        System.out.println("====================操作如下：========================");
        System.out.println("A： 增加学生信息");
        System.out.println("D： 删除学生信息");
        System.out.println("M： 修改学生信息");
        System.out.println("S： 查找学生信息");
        System.out.println("ALL： 查看所有学生信息");
        System.out.println("Q： 退出");
        System.out.println("====================================================");
        //获取用户的输入
        Scanner scanner = new Scanner(System.in);
        //加载文件中的学生信息放在list集合中
        File file = new File("D:/student.txt");
        // 如果文件为空不读取数据
        if (file.length() != 0) {
            ObjectInputStream objectInputStream = null;
            try {
                objectInputStream = new ObjectInputStream(new FileInputStream("D:/student.txt"));
                try {
                    Object o = objectInputStream.readObject();
                    list = (List<StudentInformation>) o;
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != objectInputStream) {
                        objectInputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        while (true) {
            System.out.println("请输入操作：");
            String string = scanner.next();
            switch (string) {
                case "A":
                    System.out.println("请输入添加学生学号：");
                    int i = scanner.nextInt();
                    System.out.println("请输入添加学生姓名：");
                    String next = scanner.next();
                    System.out.println("请输入添加学生年龄：");
                    int j = scanner.nextInt();
                    try {
                        list.add(new StudentInformation(i, next, j));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "D":
                    System.out.println("请输入删除学生学号：");
                    int i_delete = scanner.nextInt();
                    System.out.println("请输入删除学生姓名：");
                    String next_delete = scanner.next();
                    System.out.println("请输入删除学生年龄：");
                    int j_delete = scanner.nextInt();
                    try {
                        list.remove(new StudentInformation(i_delete, next_delete, j_delete));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "M":
                    System.out.println("请输入原学生学号：");
                    int i_origin = scanner.nextInt();
                    System.out.println("请输入原学生姓名：");
                    String next_origin = scanner.next();
                    System.out.println("请输入原学生年龄：");
                    int j_origin = scanner.nextInt();
                    int i1 = 0;
                    try {
                        i1 = list.indexOf(new StudentInformation(i_origin, next_origin, j_origin));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("请输入修改后学生学号：");
                    int i_modify = scanner.nextInt();
                    System.out.println("请输入修改后学生姓名：");
                    String next_modify = scanner.next();
                    System.out.println("请输入修改后学生年龄：");
                    int j_modify = scanner.nextInt();
                    try {
                        list.set(i1, new StudentInformation(i_modify, next_modify, j_modify));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "S":
                    System.out.println("请输入查找学生学号：");
                    int i_search = scanner.nextInt();
                    System.out.println("请输入查找学生姓名：");
                    String next_search = scanner.next();
                    System.out.println("请输入查找学生年龄：");
                    int j_search = scanner.nextInt();
                    int i2 = 0;
                    try {
                        i2 = list.indexOf(new StudentInformation(i_search, next_search, j_search));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (i2 >= 0) {
                        System.out.println("查找的学生信息存在！");
                    } else {
                        System.out.println("查找的学生信息不存在！");
                    }
                    break;
                case "ALL":
                    if (list.size() == 0) {
                        System.out.println("没有学生信息！");
                    }
                    for (StudentInformation str : list) {
                        System.out.println(str);
                    }
                    break;
                case "Q":
                    System.out.println("退出操作！");
                    //如果list数据为空，不执行写入操作
                    if (list.size() != 0) {
                        //将学生信息写入到文件中D:/student.txt
                        ObjectOutputStream objectOutputStream = null;
                        try {
                            objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:/student.txt"));
                            objectOutputStream.writeObject(list);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (null != objectOutputStream) {
                                    objectOutputStream.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
                default:
                    System.out.println("不支持的操作！");
            }
            if (string.equals("Q")) {
                break;
            }
        }
        scanner.close();
    }
}
